<template>
  <div class="data-container">
    <!-- 页面头部 -->
    <div class="data-header">
      <div class="header-content">
        <div class="header-title">
          <i class="el-icon-document-copy" />
          <h2>数据文档管理</h2>
        </div>
        <p class="header-description">管理系统数据文档，支持上传JSON格式的心理问答文件</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="data-main">
      <!-- 上传区域 -->
      <el-card class="upload-card" shadow="hover">
        <div slot="header" class="card-header">
          <div class="card-title">
            <i class="el-icon-upload2" />
            <span>上传数据文档</span>
          </div>
          <el-button
            type="success"
            size="small"
            :loading="loading"
            icon="el-icon-refresh"
            @click="refreshList"
          >
            刷新列表
          </el-button>
        </div>

        <el-upload
          ref="upload"
          class="upload-dragger"
          drag
          :action="uploadUrl"
          :headers="uploadHeaders"
          :data="uploadData"
          :before-upload="beforeUpload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :on-progress="handleUploadProgress"
          :show-file-list="false"
          accept=".json"
        >
          <div class="upload-content">
            <i class="el-icon-upload upload-icon" />
            <div class="upload-text">
              将JSON文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="upload-tip">
              只能上传.json文件，且不超过5MB
            </div>
          </div>
        </el-upload>

        <!-- 上传进度 -->
        <div v-if="uploadProgress > 0 && uploadProgress < 100" class="upload-progress">
          <div class="progress-header">
            <span>上传进度</span>
            <span>{{ uploadProgress }}%</span>
          </div>
          <el-progress
            :percentage="uploadProgress"
            :status="uploadProgress === 100 ? 'success' : ''"
            :stroke-width="8"
            :show-text="false"
          />
        </div>
      </el-card>

      <!-- 文档列表区域 -->
      <el-card class="list-card" shadow="hover">
        <div slot="header" class="card-header">
          <div class="card-title">
            <i class="el-icon-document" />
            <span>数据文档列表</span>
          </div>
          <div class="document-count">
            <i class="el-icon-files" />
            <span>共 {{ documentsList.length }} 个文档</span>
          </div>
        </div>

        <div v-loading="loading" class="documents-list">
          <div v-if="documentsList.length === 0 && !loading" class="empty-list">
            <div class="empty-icon">
              <i class="el-icon-document" />
            </div>
            <p class="empty-text">暂无数据文档</p>
            <p class="empty-hint">点击上方上传区域添加第一个数据文档</p>
          </div>
          <div v-else class="documents-table">
            <el-table
              :data="documentsList"
              stripe
              style="width: 100%"
              empty-text="暂无数据"
              class="modern-table"
            >
              <el-table-column prop="id" label="ID" width="80" align="center">
                <template slot-scope="scope">
                  <el-tag size="mini" type="info">{{ scope.row.id }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="filename" label="文件名" min-width="200">
                <template slot-scope="scope">
                  <div class="filename-cell">
                    <i class="el-icon-document" />
                    <span class="filename-text">{{ scope.row.filename }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="size" label="文件大小" width="120" align="center">
                <template slot-scope="scope">
                  <span class="file-size">{{ formatFileSize(scope.row.size) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="uploadTime" label="上传时间" width="180" align="center">
                <template slot-scope="scope">
                  <span class="upload-time">{{ formatDate(scope.row.uploadTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width="100" align="center">
                <template slot-scope="scope">
                  <el-tag
                    :type="scope.row.status === 'success' ? 'success' : 'danger'"
                    size="small"
                    effect="light"
                  >
                    <i :class="scope.row.status === 'success' ? 'el-icon-check' : 'el-icon-close'" />
                    {{ scope.row.status === 'success' ? '正常' : '异常' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="160" fixed="right" align="center">
                <template slot-scope="scope">
                  <div class="action-buttons">
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-view"
                      @click="viewDocumentDetail(scope.row)"
                    >
                      查看
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      icon="el-icon-download"
                      @click="downloadDocument(scope.row)"
                    >
                      下载
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      class="danger-text"
                      icon="el-icon-delete"
                      @click="deleteDocument(scope.row)"
                    >
                      删除
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 文档详情对话框 -->
    <el-dialog
      title="文档详情"
      :visible.sync="detailDialogVisible"
      width="800px"
      class="detail-dialog"
    >
      <div v-if="currentDocument" class="document-detail">
        <div class="detail-header">
          <div class="file-info">
            <i class="el-icon-document" />
            <div class="file-details">
              <h3 class="file-name">{{ currentDocument.filename }}</h3>
              <div class="file-meta">
                <span class="file-size">{{ formatFileSize(currentDocument.size) }}</span>
                <span class="file-time">{{ formatDate(currentDocument.uploadTime) }}</span>
              </div>
            </div>
          </div>
          <el-tag
            :type="currentDocument.status === 'success' ? 'success' : 'danger'"
            size="medium"
            effect="light"
          >
            <i :class="currentDocument.status === 'success' ? 'el-icon-check' : 'el-icon-close'" />
            {{ currentDocument.status === 'success' ? '正常' : '异常' }}
          </el-tag>
        </div>

        <div class="detail-content">
          <div class="content-section">
            <h4 class="section-title">
              <i class="el-icon-view" />
              文件内容预览
            </h4>
            <div class="content-preview">
              <pre><code>{{ currentDocument.content }}</code></pre>
            </div>
          </div>
        </div>

        <div class="detail-actions">
          <el-button
            type="primary"
            icon="el-icon-download"
            @click="downloadDocument(currentDocument)"
          >
            下载文件
          </el-button>
          <el-button
            icon="el-icon-close"
            @click="detailDialogVisible = false"
          >
            关闭
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import documentMixin from '@/mixins/documentMixin'

export default {
  name: 'Data',
  mixins: [documentMixin],
  data() {
    return {
      uploadUrl: this.dataUploadUrl,
      uploadHeaders: this.dataUploadHeaders,
      uploadData: {}
    }
  },
  created() {
    this.loadDocumentsList()
  },
  methods: {
    // 上传前验证
    beforeUpload(file) {
      return this.beforeDataUpload(file)
    },

    // 上传进度
    handleUploadProgress(event) {
      this.handleDataUploadProgress(event)
    },

    // 上传成功
    handleUploadSuccess(response, file) {
      this.handleDataUploadSuccess(response, file)
    },

    // 上传失败
    handleUploadError(error) {
      this.handleDataUploadError(error)
    },

    // 刷新列表
    refreshList() {
      this.refreshDocumentsList()
    }
  }
}
</script>

<style lang="scss" scoped>
.data-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;

  @media (max-width: 768px) {
    padding: 10px;
  }
}

.data-header {
  margin-bottom: 24px;

  .header-content {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(0, 0, 0, 0.05);

    .header-title {
      display: flex;
      align-items: center;
      margin-bottom: 8px;

      i {
        font-size: 24px;
        color: #409eff;
        margin-right: 12px;
      }

      h2 {
        margin: 0;
        font-size: 24px;
        font-weight: 600;
        color: #303133;
        letter-spacing: 0.5px;
      }
    }

    .header-description {
      margin: 0;
      color: #606266;
      font-size: 14px;
      line-height: 1.6;
    }
  }
}

.data-main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;

  @media (min-width: 1200px) {
    grid-template-columns: 1fr 2fr;
  }
}

.upload-card,
.list-card {
  border-radius: 12px;
  border: none;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  }

  ::v-deep .el-card__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 12px 12px 0 0;
    padding: 16px 20px;
    border: none;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .card-title {
        display: flex;
        align-items: center;

        i {
          font-size: 18px;
          margin-right: 8px;
        }

        span {
          font-size: 16px;
          font-weight: 600;
        }
      }

      .document-count {
        display: flex;
        align-items: center;
        font-size: 14px;
        opacity: 0.9;

        i {
          margin-right: 6px;
          font-size: 16px;
        }
      }
    }
  }

  ::v-deep .el-card__body {
    padding: 24px;
  }
}

.upload-dragger {
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  background: #fafafa;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;

  &:hover {
    border-color: #409eff;
    background: #f0f9ff;
  }

  ::v-deep .el-upload-dragger {
    width: 100%;
    height: 200px;
    border: none;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    .upload-content {
      text-align: center;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      padding: 20px;

      .upload-icon {
        font-size: 48px;
        color: #c0c4cc;
        margin-bottom: 16px;
        display: block;
        transition: all 0.3s ease;
      }

      .upload-text {
        font-size: 16px;
        color: #606266;
        margin-bottom: 8px;
        line-height: 1.5;

        em {
          color: #409eff;
          font-style: normal;
          font-weight: 500;
        }
      }

      .upload-tip {
        font-size: 12px;
        color: #909399;
        line-height: 1.4;
      }
    }
  }

  &:hover ::v-deep .el-upload-dragger .upload-content .upload-icon {
    color: #409eff;
    transform: scale(1.1);
  }
}

.upload-progress {
  margin-top: 16px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;

  .progress-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 14px;
    color: #606266;
  }
}

.documents-list {
  .empty-list {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    text-align: center;

    .empty-icon {
      margin-bottom: 16px;

      i {
        font-size: 64px;
        color: #c0c4cc;
      }
    }

    .empty-text {
      font-size: 16px;
      color: #909399;
      margin: 0 0 8px 0;
      font-weight: 500;
    }

    .empty-hint {
      font-size: 14px;
      color: #c0c4cc;
      margin: 0;
    }
  }
}

.modern-table {
  ::v-deep .el-table {
    border-radius: 8px;
    overflow: hidden;

    .el-table__header {
      background: #f8f9fa;

      th {
        background: #f8f9fa;
        color: #606266;
        font-weight: 600;
        border-bottom: 2px solid #e9ecef;
      }
    }

    .el-table__body {
      tr {
        transition: all 0.3s ease;

        &:hover {
          background: #f0f9ff;
        }
      }
    }
  }
}

.filename-cell {
  display: flex;
  align-items: center;

  i {
    font-size: 16px;
    color: #409eff;
    margin-right: 8px;
  }

  .filename-text {
    font-weight: 500;
    color: #303133;
  }
}

.file-size {
  font-weight: 500;
  color: #606266;
}

.upload-time {
  font-size: 13px;
  color: #909399;
}

.action-buttons {
  display: flex;
  gap: 4px;

  .el-button {
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 4px;
    transition: all 0.3s ease;

    &:hover {
      background: #f5f7fa;
      transform: translateY(-1px);
    }

    &.danger-text {
      color: #f56c6c;

      &:hover {
        background: #fef0f0;
        color: #f56c6c;
      }
    }
  }
}

// 详情对话框样式
.detail-dialog {
  ::v-deep .el-dialog {
    border-radius: 12px;
    overflow: hidden;
  }

  ::v-deep .el-dialog__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px 24px;

    .el-dialog__title {
      font-weight: 600;
    }
  }

  .document-detail {
    .detail-header {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      margin-bottom: 24px;
      padding-bottom: 16px;
      border-bottom: 1px solid #e9ecef;

      .file-info {
        display: flex;
        align-items: center;

        i {
          font-size: 32px;
          color: #409eff;
          margin-right: 16px;
        }

        .file-details {
          .file-name {
            font-size: 18px;
            font-weight: 600;
            color: #303133;
            margin: 0 0 8px 0;
          }

          .file-meta {
            display: flex;
            gap: 16px;
            font-size: 14px;
            color: #909399;

            .file-size {
              font-weight: 500;
            }
          }
        }
      }
    }

    .detail-content {
      .content-section {
        .section-title {
          display: flex;
          align-items: center;
          font-size: 16px;
          font-weight: 600;
          color: #303133;
          margin: 0 0 12px 0;

          i {
            margin-right: 8px;
            color: #409eff;
          }
        }

        .content-preview {
          background: #f8f9fa;
          border-radius: 8px;
          padding: 16px;
          border: 1px solid #e9ecef;
          max-height: 300px;
          overflow-y: auto;

          pre {
            margin: 0;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
            font-size: 13px;
            line-height: 1.6;
            color: #495057;

            code {
              background: none;
              padding: 0;
              color: inherit;
              font-size: inherit;
            }
          }
        }
      }
    }

    .detail-actions {
      display: flex;
      justify-content: flex-end;
      gap: 12px;
      margin-top: 24px;
      padding-top: 16px;
      border-top: 1px solid #e9ecef;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .data-header .header-content {
    padding: 16px;

    .header-title h2 {
      font-size: 20px;
    }
  }

  .data-main {
    grid-template-columns: 1fr;
  }

  .upload-card,
  .list-card {
    ::v-deep .el-card__body {
      padding: 16px;
    }
  }

  .upload-dragger {
    min-height: 150px;

    ::v-deep .el-upload-dragger {
      height: 150px;

      .upload-content {
        .upload-icon {
          font-size: 36px;
          margin-bottom: 12px;
        }

        .upload-text {
          font-size: 14px;
        }
      }
    }
  }

  .action-buttons {
    flex-direction: column;
    gap: 2px;

    .el-button {
      font-size: 11px;
      padding: 2px 6px;
    }
  }

  .detail-dialog {
    ::v-deep .el-dialog {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  .document-detail {
    .detail-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 12px;

      .file-info {
        .file-details .file-meta {
          flex-direction: column;
          gap: 4px;
        }
      }
    }

    .detail-actions {
      flex-direction: column;

      .el-button {
        width: 100%;
      }
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.upload-card,
.list-card {
  animation: fadeInUp 0.6s ease-out;
}

.list-card {
  animation-delay: 0.2s;
}

// 滚动条样式
.content-preview::-webkit-scrollbar {
  width: 6px;
}

.content-preview::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.content-preview::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;

  &:hover {
    background: #a8a8a8;
  }
}
</style>

